//
//  AppDelegate+Intros.m
//  lanrenzhoumo
//
//  Created by bilibili on 16/4/15.
//  Copyright © 2016年 吕嘉麟. All rights reserved.
//

#import "AppDelegate+Intros.h"

NSTimer *_timer;
UIView *_backView;
UIScrollView *_introsScrollView;
@implementation AppDelegate (Intros)

-(void)showIntros{
    //创建底部视图
    UIView *backView = [[UIView alloc]initWithFrame:_mainNavigation.view.bounds];
    _backView = backView;
    [_mainNavigation.view addSubview:backView];
    
    //创建scrollView
    UIScrollView *introsScrollView = [[UIScrollView alloc]initWithFrame:backView.bounds];
    //设置分页
    _introsScrollView = introsScrollView;
    //关闭自带滚动
    [introsScrollView setScrollEnabled:NO];
    
    
    [introsScrollView setPagingEnabled:YES];
    //设置滚动范围
    [introsScrollView setContentSize:CGSizeMake(introsScrollView.current_w*4, 0)];
    //设置偏移量
    [introsScrollView setContentOffset:CGPointMake(0, 0)];
    //关闭bounce效果
    [introsScrollView setBounces:NO];
    //关闭水平滚动条
    [introsScrollView setShowsHorizontalScrollIndicator:NO];
    
    [backView addSubview:introsScrollView];
    
    //
    NSArray *textArray = @[@"闭目，难掩喜悦与期待。",@"睁眼，因为你心为所动。",@"启程，只因追寻你所爱。",@"我们，做最了解你的人。"];
    for (NSInteger i=0; i<4; i++) {
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*introsScrollView.current_w, 0, introsScrollView.current_w, introsScrollView.current_h)];
        
        [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",i+1]]];
        
        [introsScrollView addSubview:imageView];
        
        UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, introsScrollView.current_h-154, introsScrollView.current_w, 24)];
        [textLabel setTextColor:[UIColor whiteColor]];
        [textLabel setBackgroundColor:[UIColor clearColor]];
        [textLabel setFont:[UIFont systemFontOfSize:18]];
        [textLabel setTextAlignment:NSTextAlignmentCenter];
        [textLabel setText:textArray[i]];
        [imageView addSubview:textLabel];
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerResponse) userInfo:nil repeats:YES];
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureResponse:)];
    
    [_introsScrollView addGestureRecognizer:panGesture];
    
    //创建微博登陆按钮
    UIButton *sinaLoginBtn = [self createLoginBtnWithFrame:CGRectMake(0, backView.current_h-80, (backView.current_w-2)/2.0, 40) icon:[UIImage imageNamed:@"sina.png"] title:@"微博登陆"];
    //创建微信登陆按钮
    [self createLoginBtnWithFrame:CGRectMake(sinaLoginBtn.current_w+2, sinaLoginBtn.current_y, sinaLoginBtn.current_w, sinaLoginBtn.current_h) icon:[UIImage imageNamed:@"weixin.png"] title:@"微信登陆"];
    //创建“到处逛逛”按钮
    UIButton *skimBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, backView.current_h-40, backView.current_w, 40)];
    
    [skimBtn setBackgroundColor:[UIColor clearColor]];
    
    [skimBtn setTitle:@"暂不登陆，随便逛逛" forState:UIControlStateNormal];
    [skimBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [skimBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [skimBtn addTarget:self action:@selector(skimAction:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:skimBtn];
}
#pragma mark -- 逛逛按钮响应事件
-(void)skimAction:(UIButton *)sender{
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.type = @"push";
    transition.subtype =@"fromRight";
    [_mainNavigation.view.layer addAnimation:transition forKey:nil];
    [_backView removeFromSuperview];
}

#pragma mark -- 创建登陆按钮
-(UIButton *)createLoginBtnWithFrame:(CGRect)frame icon:(UIImage *)icon title:(NSString *)title{
    
    UIButton *loginBtn = [[UIButton alloc]initWithFrame:frame];
    [loginBtn setTitle:title forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [loginBtn setBackgroundColor:[UIColor blackColor]];
    
    
    [loginBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [loginBtn setImage:icon forState:UIControlStateNormal];
    [loginBtn setImageEdgeInsets:UIEdgeInsetsMake(10, (loginBtn.current_w-95)/2.0+10, 10, loginBtn.current_w-(loginBtn.current_w-95)/2.0-30)];
    [loginBtn setTitleEdgeInsets:UIEdgeInsetsMake(10, (loginBtn.current_w-95)/2.0, 10, loginBtn.current_w-(loginBtn.current_w-95)/2.0-95)];
    [loginBtn addTarget:self action:@selector(loginWithThird:) forControlEvents:UIControlEventTouchUpInside];
    [_backView addSubview:loginBtn];
    
    return loginBtn;
}
#pragma mark -- 登陆按钮响应事件
-(void)loginWithThird:(UIButton *)sender{
    
}

#pragma mark -- 拖动手势响应事件
-(void)panGestureResponse:(UIPanGestureRecognizer *)panGesture{
    NSInteger currentPage = _introsScrollView.contentOffset.x/_introsScrollView.current_w;
    CATransition *transition = [CATransition animation];
    
    transition.duration = 1.0;
    transition.type = @"fade";
    [_introsScrollView.layer addAnimation:transition forKey:nil];
    
    CGFloat startX = 0;
    [_timer invalidate];
    //获取开始拖动时x坐标
    if (panGesture.state == UIGestureRecognizerStateBegan) {
        startX = [panGesture translationInView:_backView].x;
        
    }
    else if(panGesture.state == UIGestureRecognizerStateEnded){
        CGFloat endedX = [panGesture translationInView:_backView].x;
        //展示下一张
        if (endedX<startX) {
            
            //如果当前页是第4张
            if (currentPage == 3) {
                _introsScrollView.contentOffset = CGPointMake(0, 0);
            }
            else{
                 _introsScrollView.contentOffset = CGPointMake((currentPage+1)*_introsScrollView.current_w, 0);
            }
        }
        //展示上一张
        else if (endedX>startX){
            if (currentPage == 0) {
                _introsScrollView.contentOffset = CGPointMake(3*_introsScrollView.current_w, 0);
            }
            else{
                _introsScrollView.contentOffset = CGPointMake((currentPage-1)*_introsScrollView.current_w, 0);
            }
        }
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerResponse) userInfo:nil repeats:YES];
}

#pragma mark -- 定时器的响应事件
-(void)timerResponse{
    CATransition *transition = [CATransition animation];
    
    transition.duration = 1.0;
    transition.type = @"fade";
    [_introsScrollView.layer addAnimation:transition forKey:nil];
    
    NSInteger currentPage = _introsScrollView.contentOffset.x/_introsScrollView.current_w;
    if (currentPage == 3) {
        _introsScrollView.contentOffset = CGPointMake(0, 0);
    }
    else{
        _introsScrollView.contentOffset = CGPointMake((currentPage+1)*_introsScrollView.current_w, 0);
    }
}

@end
